#include "lua_obj.h"

static int lua_lv_textarea_add_char(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);
    uint32_t c = luaL_checkinteger(L, 2);
    lv_textarea_add_char(a, c);

    return 0;
}

static int lua_lv_textarea_add_text(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);

    return 0;
}

static int lua_lv_textarea_del_char(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);

    return 0;
}

static int lua_lv_textarea_del_char_forward(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);

    return 0;
}

static int lua_lv_textarea_set_text(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);

    return 0;
}

static int lua_lv_textarea_set_placeholder_text(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);

    return 0;
}

static int lua_lv_textarea_set_cursor_pos(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);
    int32_t pos = luaL_checkinteger(L, 2);
    lv_textarea_set_cursor_pos(a, pos);

    return 0;
}

static int lua_lv_textarea_set_cursor_click_pos(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);

    return 0;
}

static int lua_lv_textarea_set_password_mode(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);

    return 0;
}

static int lua_lv_textarea_set_one_line(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);

    return 0;
}

static int lua_lv_textarea_set_accepted_chars(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);

    return 0;
}

static int lua_lv_textarea_set_max_length(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);
    uint32_t num = luaL_checkinteger(L, 2);
    lv_textarea_set_max_length(a, num);

    return 0;
}

static int lua_lv_textarea_set_insert_replace(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);

    return 0;
}

static int lua_lv_textarea_set_text_selection(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);

    return 0;
}

static int lua_lv_textarea_set_password_show_time(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);
    uint16_t time = luaL_checkinteger(L, 2);
    lv_textarea_set_password_show_time(a, time);

    return 0;
}

static int lua_lv_textarea_set_align(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);

    return 0;
}

static int lua_lv_textarea_get_text(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);

    return 0;
}

static int lua_lv_textarea_get_placeholder_text(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);

    return 0;
}

static int lua_lv_textarea_get_label(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);

    return 0;
}

static int lua_lv_textarea_get_cursor_pos(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);
    uint16_t res = lv_textarea_get_cursor_pos(a);
    lua_pushinteger(L, res);

    return 1;
}

static int lua_lv_textarea_get_cursor_click_pos(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);

    return 0;
}

static int lua_lv_textarea_get_password_mode(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);

    return 0;
}

static int lua_lv_textarea_get_one_line(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);

    return 0;
}

static int lua_lv_textarea_get_accepted_chars(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);

    return 0;
}

static int lua_lv_textarea_get_max_length(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);
    uint16_t res = lv_textarea_get_max_length(a);
    lua_pushinteger(L, res);

    return 1;
}

static int lua_lv_textarea_text_is_selected(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);

    return 0;
}

static int lua_lv_textarea_get_text_selection(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);

    return 0;
}

static int lua_lv_textarea_get_password_show_time(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);
    uint16_t res = lv_textarea_get_password_show_time(a);
    lua_pushinteger(L, res);

    return 1;
}

static int lua_lv_textarea_clear_selection(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);

    return 0;
}

static int lua_lv_textarea_cursor_right(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);

    return 0;
}

static int lua_lv_textarea_cursor_left(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);

    return 0;
}

static int lua_lv_textarea_cursor_down(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);

    return 0;
}

static int lua_lv_textarea_cursor_up(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);

    return 0;
}

static int lua_auto_gc(lua_State *L)
{
    lv_obj_t *lv_obj = *(lv_obj_t **)luaL_checkudata(L, 1, META_TEXTAREA);
    luaL_argcheck(L, lv_obj != NULL, 1, "invalid user data");
    if (lv_obj)
    {
        lv_obj_del(lv_obj);
    }
    return 0;
}
static const struct luaL_Reg arraylib_m[] = {
    {"add_char", lua_lv_textarea_add_char},
    {"add_text", lua_lv_textarea_add_text},
    {"del_char", lua_lv_textarea_del_char},
    {"del_char_forward", lua_lv_textarea_del_char_forward},
    {"set_text", lua_lv_textarea_set_text},
    {"set_placeholder_text", lua_lv_textarea_set_placeholder_text},
    {"set_cursor_pos", lua_lv_textarea_set_cursor_pos},
    {"set_cursor_click_pos", lua_lv_textarea_set_cursor_click_pos},
    {"set_password_mode", lua_lv_textarea_set_password_mode},
    {"set_one_line", lua_lv_textarea_set_one_line},
    {"set_accepted_chars", lua_lv_textarea_set_accepted_chars},
    {"set_max_length", lua_lv_textarea_set_max_length},
    {"set_insert_replace", lua_lv_textarea_set_insert_replace},
    {"set_text_selection", lua_lv_textarea_set_text_selection},
    {"set_password_show_time", lua_lv_textarea_set_password_show_time},
    {"set_align", lua_lv_textarea_set_align},
    {"get_text", lua_lv_textarea_get_text},
    {"get_placeholder_text", lua_lv_textarea_get_placeholder_text},
    {"get_label", lua_lv_textarea_get_label},
    {"get_cursor_pos", lua_lv_textarea_get_cursor_pos},
    {"get_cursor_click_pos", lua_lv_textarea_get_cursor_click_pos},
    {"get_password_mode", lua_lv_textarea_get_password_mode},
    {"get_one_line", lua_lv_textarea_get_one_line},
    {"get_accepted_chars", lua_lv_textarea_get_accepted_chars},
    {"get_max_length", lua_lv_textarea_get_max_length},
    {"text_is_selected", lua_lv_textarea_text_is_selected},
    {"get_text_selection", lua_lv_textarea_get_text_selection},
    {"get_password_show_time", lua_lv_textarea_get_password_show_time},
    {"clear_selection", lua_lv_textarea_clear_selection},
    {"cursor_right", lua_lv_textarea_cursor_right},
    {"cursor_left", lua_lv_textarea_cursor_left},
    {"cursor_down", lua_lv_textarea_cursor_down},
    {"cursor_up", lua_lv_textarea_cursor_up},
    LUA_LV_OBJ_RLT,
    {NULL, NULL}};

static int help(lua_State *L)
{
    uint16_t n = sizeof(arraylib_m) / sizeof(struct luaL_Reg);
    for (int i = 0; i < n - 1; i++)
    {
        printf("%s\n", arraylib_m[i].name);
    }
    return 1;
}

static const struct luaL_Reg arraylib_f[] = {
    {"help", help},
    {NULL, NULL}};

LUALIB_API int lua_lv_textarea_funinit(lua_State *L)
{
    luaL_newlib(L, arraylib_f);
    luaL_newmetatable(L, META_TEXTAREA);  /* create metatable for file handles */
    lua_pushvalue(L, -1);            /* push metatable */
    lua_setfield(L, -2, "__index");  /* metatable.__index = metatable */
    luaL_setfuncs(L, arraylib_m, 0); /* add file methods to new metatable */
    lua_pop(L, 1);                   /* pop new metatable */

    return 1;
}